/**
 * PHOSIDE: PHosphorylation Site IDentification Engine.
 * Copyright 2009 Digital Biology Lab, University of Missouri.
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version. <p/> This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more
 * details.
 */

package phoside.miscellaneous.io;

import java.io.IOException;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import phoside.PhosphoProteins;
import phoside.PhosphoProteinsImpl;
import phoside.Protein;
import phoside.ProteinFilter;

import phoside.io.ProteinsReader;

import phoside.util.IOUtil;
import phoside.util.TextUtil;

/**
 *
 * @author gjj
 */
public class PhosphoReaderPhosphoELM implements ProteinsReader {
    private PhosphoProteins data;
    private String elm;
    private String kinase;

    public PhosphoReaderPhosphoELM(String elm, PhosphoProteins data, String kinase) {
        this.elm = elm;
        this.data = data;
        this.kinase = null;
        if (kinase!=null)
            this.kinase = kinase.toLowerCase();
    }

    public PhosphoProteins read() {
        List<String> lines;
        try {
             lines = IOUtil.readStringListAscii(elm);
        } catch(IOException ex) {
            ex.printStackTrace();
            return data;
        }

        int n = lines.size();
        for (int i=1; i<n; i++) {
            String line = lines.get(i);
            String[] strs = line.split("\t");
            String acc = strs[0];
            String seq = strs[1];
            String pos = strs[2];

            if (kinase!=null && strs[5].toLowerCase().indexOf(kinase)==-1)
                continue;

            List<String> notes = null;
            if (strs[5].trim().length()>0) {
                String strKinase = "kinase:"+strs[5].trim().replaceAll(";", ",");
                notes = new ArrayList();
                notes.add(strKinase);
            }

            Protein protein = data.getProtein(acc);
            if (protein==null) {
                protein = new phoside.ProteinImpl(acc,seq,null,null,null);
                data.addProtein(protein);
            }

            try {
                data.addSite(acc, Integer.valueOf(pos)-1, notes);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return data;
    }

    public static void main(String[] args) {
//        java.util.Map<String,Integer> map;
//        try {
//            map = TextUtil.countFrequency("D:\\projects\\PHOSIDE\\PHOSIDE\\data\\phosphoelm\\kinases");
//            List<String> kinaseList = new java.util.ArrayList();
//            for (String kinase : map.keySet()) {
//                kinaseList.add(kinase+"\t"+map.get(kinase));
//            }
//            IOUtil.writeListAscii(kinaseList, "D:\\projects\\PHOSIDE\\PHOSIDE\\data\\phosphoelm\\kinases-frequency.txt");
//        } catch (IOException e) {
//            e.printStackTrace();
//            return;
//        }


        String direlm = "D:\\projects\\PHOSIDE\\PHOSIDE\\data\\phosphoelm\\phosphoELM_0409";
        //String filter = "D:\\projects\\PHOSIDE\\PHOSIDE\\data\\phosphoelm\\phosphoELM_0409.acc.HUMAN";
        String out = "D:\\projects\\PHOSIDE\\PHOSIDE\\data\\phosphoelm\\PhosphoELM-V8.2-April09.phoside.xml";
        String kinase = null;//"PKA";
        PhosphoProteins data = new PhosphoProteinsImpl();
        PhosphoReaderPhosphoELM reader = new PhosphoReaderPhosphoELM(direlm, data, kinase);
        reader.read();

        try {
//            final Set<String> accs = IOUtil.readStringSetAscii(filter);
//            data.filterProteins(new ProteinFilter() {
//                public boolean filter(Protein protein) {
//                    return accs.contains(protein.getAccession());
//                }
//            });

//            phoside.io.fasta.PhosphoWriterToFastaWithSitesInSequence writer
//                = new phoside.io.fasta.PhosphoWriterToFastaWithSitesInSequence(out);
            phoside.io.xml.PhosideXmlProteinsWriter writer
                    = new phoside.io.xml.PhosideXmlProteinsWriter(out,
                    new phoside.io.xml.PhosphoProteinFieldValueFormatter());
            writer.write(data);
        } catch(IOException e) {
            e.printStackTrace();
        }
    }
}
